<?php

include_once APPLICATION_DIR . "/application/model/LOGIN.php";

class Login extends DataObjects_LOGIN
{
	/**
	 * Enter description here...
	 *
	 * @deprecated version 0.0.0 - Sep 11, 2007
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	function getFromId($id) {
		$this->ID = $id;
		$this->whereAdd("DISABLED != 1");
		$this->find();
		return $this->fetch();

	}
    
    /**
	 * Check if email of the user exist
	 *
	 * @param string $email
	 * @return int $id is the Login ID
	 */
	function userExistByEmail($email) {

		$t = new Login();
		$t->EMAIL = $email;
		$t->DISABLED=0;
		$t->find(1);
		return $t->ID;
	}
    
    /**
	 * Check if EMAIL of the user exist
	 *
	 * @param string $email
	 * @param string $loginID
	 * @return int $id is the Login ID
	 */
	function emailExistByLoginID($email, $loginID) {
		$t = new Login();
		$t->EMAIL = $email;
		if (trim($loginID) != '' && $loginID != 0)
		{
			$t->whereAdd("ID <> " . $loginID);
		}
		$t->DISABLED=0;
		$t->find(1);
		return $t->ID > 0;
	}
    
    /**
	 * Insert/Update the Login
	 *
	 * @return data_object updated
	 */
	function store() {
		if ($this->ID==0)
			return $this->insert();
		else return $this->update();
	}
    
    /**
	 * Update Date Last Login to current date if user login to the system
	 *
	 * @return data_object updated
	 */
	function updateLogin() {
		$this->LAST_LOGIN = date("Y-m-d G:i:s");
		$this->update();
	}
    
    function deletable(){
        if($this->ID == $_SESSION['userID']){
            return false;
        }
        return true;
    }
    
    /**
	 * Check whether or not account with specific ID login is exist
	 *
	 * @return boolean true if exist, otherwise false
	 */
	function isExistLoginID()
	{
		$login = new Login();

		$login->whereAdd('ID = ' . $_SESSION['userID']);
		$login->whereAdd('DISABLED = 0');
		if ($login->count() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

    function isDeactivated($login)
	{
		$bln_ok = false;

		if ($login)
		{
			$obj_login = new Login();
			$obj_login->EMAIL = $login;
			$obj_login->find();
			if ($obj_login->fetch())
			{
				if ($obj_login->DISABLED)
				{
					$bln_ok = true;
				}
			}
		}

		return $bln_ok;
	}
    
    function isAllowedAccessProject($project = USER_PROJECT_ACCESS_ALLOWED_VODAFONELIVE) {
        return true;
    }
	
	function isSupervisor() {
		return isset($this->IS_SUPERVISOR) && ($this->IS_SUPERVISOR == 1);
	}

    public function validateInput(){
        global $error_msg;
        
        if(!textvalid($this->FIRST_NAME)){
            $error_msg->add(stripslashes(_LB_FIRSTNAME_IS_EMPTY));
        }
        if(!textvalid($this->LAST_NAME)){
            $error_msg->add(stripslashes(_LB_LASTNAME_IS_EMPTY));
        }
        if(!textvalid($this->EMAIL)){
            $error_msg->add(stripslashes(_LB_EMAIL_IS_EMPTY));
        }
        
        if(strlen($this->EMAIL)> MAX_LENGTH_NAME){
            $error_msg->add(stripslashes(_MSG_EMAIL_IS_TOO_LONG));
        }
        if(!$this->ID){
            $pass = Request::get("PASS");
            $repass = Request::get("REPASS");
            if(!textvalid($pass)){
                $error_msg->add(stripslashes(_LB_PASSWORD_IS_EMPTY));
            }
            if(strlen($pass)> MAX_LENGTH_PASSWORD){
                $error_msg->add(stripslashes(_MSG_PASSWORD_IS_TOO_LONG));
            }
            if(strlen($pass)< MIN_LENGTH_PASSWORD){
                $error_msg->add(stripslashes(_MSG_PASSWORD_IS_TOO_SHORT));
            }
            if($pass != $repass){
                $error_msg->add(stripslashes(_LB_PASSWORD_DOESNT_MATCH));
            }
        }
        return $error_msg->isNoError();
    }
    public function validateChangePassInput(){
        global $error_msg;
        
        $pass = Request::get("PASS");
        $repass = Request::get("REPASS");
        if(!textvalid($pass)){
            $error_msg->add(stripslashes(_LB_PASSWORD_IS_EMPTY));
        }
        if(strlen($pass)> MAX_LENGTH_PASSWORD){
            $error_msg->add(stripslashes(_MSG_PASSWORD_IS_TOO_LONG));
        }
        if(strlen($pass)< MIN_LENGTH_PASSWORD){
            $error_msg->add(stripslashes(_MSG_PASSWORD_IS_TOO_SHORT));
        }
        if($pass != $repass){
            $error_msg->add(stripslashes(_LB_PASSWORD_DOESNT_MATCH));
        }
        return $error_msg->isNoError();
    }
}

?>
